<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'好友维度'),
	array('op'=>'qun','title'=>'社群维度'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_scrm_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			//创建时间
			$params=array('workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']);
			if($_RQ['createtime']){
				$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
				$time_range=json_decode($time_range,true);
				$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
				$return['params']['createtime']=$time_range;
			}
			$item['newusernum']=pdo_count('jy_scrm_member',$params);
			$item['heiusernum']=pdo_count('jy_scrm_member',array('status'=>2,'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['genjinnum']=pdo_count('jy_scrm_member',array('buystatus >'=>0,'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['famsgnum']='';
			$item['huirate']='';
			$item['pingrate']='';
			$params=array('workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']);
			if($_RQ['createtime']){
				$params['createtime <=']=$time_range['tstart'];
				$item['usernum1']=pdo_count('jy_scrm_member',array('workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$params['createtime <=']=$time_range['tend'];
				$item['usernum2']=pdo_count('jy_scrm_member',array('workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['usernum3']=$item['usernum2']-$item['usernum1'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'usernum','title'=>'好友总数','type'=>'string'),
			array('name'=>'newusernum','title'=>'新增好友数','type'=>'string'),
			array('name'=>'heiusernum','title'=>'被删除/拉黑数','type'=>'string'),
			array('name'=>'usernum3','title'=>'好友数变化','type'=>'string'),
			array('name'=>'genjinnum','title'=>'员工跟进人数','type'=>'string'),
			array('name'=>'famsgnum','title'=>'员工发信息条数','type'=>'string'),
			array('name'=>'huirate','title'=>'已回复聊天占比','type'=>'string'),
			array('name'=>'pingrate','title'=>'平均首次回复时长','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'select','width'=>15,'name'=>'id','options'=>'workers','title'=>'员工姓名','option_title'=>'name'),
			array('name'=>'createtime','title'=>'统计时间段','type'=>'time_range','width'=>15),
		),
	);
	$return['workers']=pdo_getall('jy_scrm_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='qun'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_scrm_qun',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			if(!$item['workerid']){
				$item['workerid']=model('jy_scrm/worker')->get_workerid($item['owner']);
				pdo_update('jy_scrm_qun',array('workerid'=>$item['workerid']),array('id'=>$item['id']));
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'群名称','type'=>'string'),
			array('type'=>'select','name'=>'workerid','options'=>'workers','title'=>'群主姓名','option_title'=>'name'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'num','title'=>'群成员数','type'=>'string'),
			array('name'=>'usernum','title'=>'入群人数','type'=>'string'),
			array('name'=>'usernum','title'=>'退群人数','type'=>'string'),
			array('name'=>'usernum','title'=>'群人数变化','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'群名称','type'=>'string'),
			array('type'=>'select','width'=>15,'name'=>'workerid','options'=>'workers','title'=>'群主姓名','option_title'=>'name'),
			array('name'=>'createtime','title'=>'统计时间段','type'=>'time_range','width'=>15),
		),
	);
	$return['workers']=pdo_getall('jy_scrm_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;	
	exi($return);
}
